<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-input v-model="queryParams.fWarmBoxCode" placeholder="请输入温箱编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="添加日期">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:post:add']">新增温箱</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="fId" />
      <el-table-column label="温箱编码" align="center" prop="fWarmBoxCode" />
      <el-table-column label="温箱名称" align="center" prop="fWarmBoxName" />
      <el-table-column label="温箱识别编码" align="center" prop="fIdentifyNum" width="120"/>
      <el-table-column label="所属工厂" align="center" prop="bcmOrg.fOrgName" width="150"/>
      <el-table-column label="所属设备" align="center" prop="wfDevice.fDeviceName" width="120" />
      <el-table-column label="厂家名称" align="center" prop="fSupplierName" width="120" />
      <el-table-column label="品牌名称" align="center" prop="fDeviceBrand" width="120" />
      <el-table-column label="型号" align="center" prop="fDeviceModel" width="120" />
      <el-table-column label="温箱当前(℃)" align="center" prop="fNowNum" width="120" />
      <el-table-column label="最高温度(℃)" align="center" prop="fMaxNum" width="120" />
      <el-table-column label="最低温度(℃)" align="center" prop="fMinNum" width="120" />
      <el-table-column label="状态" align="center" prop="fForbidStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.fForbidStatus" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="fCreaterName" width="120" />
      <el-table-column label="创建日期" align="center" prop="fCreateDate" width="180" />
      <el-table-column label="最后修改人" align="center" prop="fModifierName" width="120" />
      <el-table-column label="最后修改日期" align="center" prop="fModifyDate" width="180" />
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 1)"
            v-hasPermi="['system:post:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="85%" @close="closeDialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="30%">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="所属工厂" prop="fOrgId">
              <el-select v-loadmore="factoryLoad" v-model="form.fOrgId" placeholder="请选择" @change="changeFactory">
                <el-option v-for="(item, index) in factoryList" :key="index" :label="item.fOrgName"
                  :value="item.fId"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="厂家名称" prop="fSupplierName">
              <el-input v-model="form.fSupplierName" placeholder="请输入" :disabled="isEdit == 1"></el-input>
            </el-form-item>
            <el-form-item label="最高温度(°C)" prop="fMaxNum">
              <el-input v-model="form.fMaxNum" placeholder="请输入" :disabled="isEdit == 1"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属设备">
              <el-select v-loadmore="deviceLoad" v-model="form.fDeviceId" placeholder="请选择">
                <el-option v-for="(item, index) in deviceList" :key="index" :label="item.fDeviceName"
                  :value="item.fId"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="品牌名称">
              <el-input v-model="form.fDeviceBrand" placeholder="请输入" :disabled="isEdit == 1"></el-input>
            </el-form-item>
            <el-form-item label="最低温度(℃)">
              <el-input v-model="form.fMinNum" placeholder="请输入" :disabled="isEdit == 1"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="温箱名称" prop="fWarmBoxName">
              <el-input v-model="form.fWarmBoxName" placeholder="请输入 " :disabled="isEdit == 1"></el-input>
            </el-form-item>
            <el-form-item label="型号">
              <el-input v-model="form.fDeviceModel" placeholder="请输入" :disabled="isEdit == 1"></el-input>
            </el-form-item>
            <el-form-item label="插槽数量" prop="fSlotNum">
              <el-input v-model="form.fSlotNum" placeholder="请输入" :disabled="isEdit == 1"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="温箱识别编号" prop="fIdentifyNum">
              <el-input v-model="form.fIdentifyNum" placeholder="请输入" :disabled="isEdit == 1"></el-input>
            </el-form-item>
            <el-form-item label="是否启用" prop="fForbidStatus">
              <el-radio-group v-model="form.fForbidStatus" :disabled="isEdit == 1">
                <el-radio v-for="dict in fForbidStatus" :key="dict.value" :label="dict.value">{{ dict.label
                  }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="产品图片">
              <div class="grid-content bg-purple-light">
                <el-form-item prop="fPictureUrl">只能上传jpg/png文件，且不超过500kb
                  <!-- <img :src="form.fPictureUrl" v-if="form.fPictureUrl" @click="dialogImageUrl = form.fPictureUrl; dialogVisible = true;"> -->
                  <ImageUpload :value="form.fPictureUrl" :limit="1" :isShowTip="false" @input="uploadFile($event)"  :disabled="isEdit == 1"/>
                </el-form-item>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="温箱编码" prop="fWarmBoxCode">
              <el-input v-model="form.fWarmBoxCode" placeholder="请输入" :disabled="isEdit == 1"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备注" prop="fRemark">
              <el-input v-model="form.fRemark" type="textarea" placeholder="请输入内容" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isEdit != 1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible" title="预览" width="800" append-to-body>
      <img :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto" />
    </el-dialog>
  </div>
</template>

<script>
import { listBox, getBox, delBox, addBox, updateBox } from "@/api/basicData/box";
import { listOrg } from "@/api/basicData/factory"
import { listDevice } from "@/api/basicData/equipment"
export default {
  name: "Box",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      dialogVisible: false,
      dialogImageUrl: '',
      // 初始化为不禁用
      uploadDisabled: false,
      isEdit: 0,
      value: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fWarmBoxCode: undefined,
        fWarmBoxName: undefined,
        status: undefined
      },
      dateRange: "",
      // 表单参数
      form: {
        fForbidStatus: 0,
        fDeviceId: '',
      },
      // 表单校验
      rules: {
        fOrgId: [
          { required: true, message: "所属工厂不能为空", trigger: "blur" }
        ],
        fDeviceId: [
          { required: true, message: "所属设备不能为空", trigger: "blur" }
        ],
        fWarmBoxName: [
          { required: true, message: "温箱名称不能为空", trigger: "blur" }
        ],
        fWarmBoxCode: [
          { required: true, message: "温箱编码不能为空", trigger: "blur" }
        ],
        fIdentifyNum: [
          { required: true, message: "温箱识别编号不能为空", trigger: "blur" }
        ],
        fSlotNum: [
          { required: true, message: "插槽数量不能为空", trigger: "blur" }
        ],
        fForbidStatus: [
          { required: true, message: "请选择是否启用不能选空", trigger: "change" }
        ],
      },
      fForbidStatus: [
        { value: 0, label: '启用' },
        { value: 1, label: '停用' }
      ],
      factoryWhere: { pageNum: 1, pageSize: 20 },
      deviceWhere: { pageNum: 1, pageSize: 20 },
      factoryList: [],
      deviceList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    uploadFile(e) {
      this.form.fPictureUrl = e

    },
    addDateRange(params, dateRange) {
      params.fCreateDateStart = dateRange[0];
      params.fCreateDateEnd = dateRange[1];
      return params;
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listBox(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单
    reset() {
      this.form = {
        fId: undefined,
        pageNum: 1,
        pageSize: 10,
        postSort: 0,
        status: "0",
        remark: undefined,
        fForbidStatus:0,
        fDeviceId: '',

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isEdit = 0
      this.reset();
      this.getFactory()
      this.open = true;
      this.title = "新增温箱";
    },
    closeDialog() {
      this.factoryWhere.pageNum = 1
      this.deviceWhere.pageNum = 1
      this.factoryList = []
      this.deviceList = []
    },
    getFactory() {
      listOrg(this.factoryWhere).then(res => {
        this.factoryList = [...this.factoryList, ...res.rows]
      })
    },
    changeFactory(e) {
      this.getDevices(e)
      this.form.fDeviceId = ''
    },
    getDevices(fOrgId) {
      this.deviceWhere.fOrgId = fOrgId
      listDevice(this.deviceWhere).then(res => {
        this.deviceList = res.rows
      })
    },
    getDevice() {
      listDevice(this.deviceWhere).then(res => {
        this.deviceList = [...this.deviceList, ...res.rows]
      })
    },
    factoryLoad() {
      this.factoryWhere.pageNum += 1
      this.getFactory()
    },
    deviceLoad() {
      this.deviceWhere.pageNum += 1
      this.getDevice()
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.isEdit = type
      this.uploadDisabled = true;
      this.getFactory()
      this.reset();
      const fId = row.fId || this.ids
      getBox(fId).then(response => {
        this.form = response.data;
        this.open = true;
        if (type === 1) {
          this.open = true;
          this.title = "查看温箱";
        } else {
          this.open = true;
          this.title = "修改温箱";
        }       
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fId != undefined) {
            updateBox(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBox(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.fId || this.ids;
      this.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function () {
        return delBox(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>